﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
using System.Collections.ObjectModel;
using SettlersEngine.Exported;

namespace SettlersEngine
{
    /// <summary>
    /// Interaction logic for RomanConfig.xaml
    /// </summary>
    public partial class RomanConfig : UserControl, IRaceConfig
    {
        public ReadOnlyCollection<BuildingConfig> Buildables { get; private set; }

        public RomanConfig()
        {
            InitializeComponent();


            String[] buildableNames = (from e in Resources.Keys.ToObjects() select (String)e).ToArray();
            Buildables = new ReadOnlyCollection<BuildingConfig>((from e in Resources.Values.ToObjects() where e is BuildingConfig select (BuildingConfig)e).ToArray());

            for (int x = 0; x < buildableNames.Length; x++)
            {
                BuildingConfig buildable = Buildables[x];

                buildable.Name = buildable.AnimationClass = buildableNames[x];

                AnimationClass animClass = AnimatedElement.Library.FindClass(buildable.AnimationClass);
                int pixelWidth = animClass.AmbientSet.Width;
                int pixelHeight = animClass.AmbientSet.Height;

                buildable.VisualWidth = (int)(pixelWidth / GameInstance.POSITION_SCALE);
                buildable.VisualHeight = (int)(pixelHeight / GameInstance.POSITION_SCALE);


                int minYSolid = buildable.VisualHeight;
                foreach (var solid in animClass.SolidBoundaries)
                {
                    int left = (int)(solid.Left / GameInstance.POSITION_SCALE);
                    int top = (int)(solid.Top / GameInstance.POSITION_SCALE);

                    System.Drawing.Rectangle intSolid = new System.Drawing.Rectangle(
                        left,
                        top,
                        (int)(Math.Ceiling((solid.Left - left * GameInstance.POSITION_SCALE + solid.Width) / GameInstance.POSITION_SCALE)),
                        (int)(Math.Ceiling((solid.Top - top * GameInstance.POSITION_SCALE + solid.Height) / GameInstance.POSITION_SCALE)));

                    if (intSolid.Top < minYSolid)
                        minYSolid = intSolid.Top;

                    buildable.SolidBoundaries.Add(intSolid);
                }

                buildable.LockedBounds = new System.Drawing.Rectangle(0, minYSolid, buildable.VisualWidth, buildable.VisualHeight - minYSolid);

                for(int i = 0; i < buildable.ResourceStacks.Count; i++)
                {
                    ResourceStackConfig stackCfg = buildable.ResourceStacks[i];

                    stackCfg.MaxStackSize = (stackCfg.MaxStackSize == 0) ? RuntimeMap.MAX_RESOURCE_STACK : stackCfg.MaxStackSize;

                    stackCfg.DoublePosition = new Point(
                        animClass.ResourceStacks[i].X / GameInstance.POSITION_SCALE,
                        animClass.ResourceStacks[i].Y / GameInstance.POSITION_SCALE);
                }
            }
        }
    }
}
